package com.example.fitapp.activity.fragment;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.bumptech.glide.Glide;
import com.example.fitapp.activity.R;
import com.example.fitapp.activity.activity.BodyDataActivity;
import com.example.fitapp.activity.activity.LoginActivity;
import com.example.fitapp.activity.activity.MainActivityWithTap;
import com.example.fitapp.activity.activity.MyApplication;
import com.example.fitapp.activity.activity.UsActivity;
import com.example.fitapp.activity.activity.UsEditActivity;
import com.example.fitapp.activity.bean.MySerializable;
import com.example.fitapp.activity.bean.UserInfoBean;
import com.example.fitapp.activity.http.api.user.UserEditHeader;
import com.example.fitapp.activity.http.api.user.UserGetInfo;
import com.example.fitapp.activity.http.util.ApiListener;
import com.example.fitapp.activity.http.util.ApiUtil;
import com.example.fitapp.activity.splash.SplashActivity;
import com.example.fitapp.activity.utils.L;
import com.google.gson.Gson;

import static android.content.Context.MODE_PRIVATE;

public class UsFragment extends Fragment {
    private static final String BUNDLE_KEY_TITLE = "key_title";
    private LinearLayout usInfo;
    private Button buttonExit;
    private LinearLayout bodyInfo;
    private TextView user_name;
    private ImageView user_header;
    private UserInfoBean userInfoBean;

    public static UsFragment newInstance(String title){
        Bundle bundle = new Bundle();
        bundle.putString(BUNDLE_KEY_TITLE,title);
        UsFragment usFragment = new UsFragment();
        usFragment.setArguments(bundle);
        return usFragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_us,container,false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        usInfo = view.findViewById(R.id.us_info);
        bodyInfo = view.findViewById(R.id.body_info);
        buttonExit = view.findViewById(R.id.button_exit);
        user_header = view.findViewById(R.id.user_header);

        user_name = (TextView) view.findViewById(R.id.user_name);
                usInfo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MySerializable mySerializable = new MySerializable();
                mySerializable.setUser_header(userInfoBean.getData().getHeader());
                mySerializable.setName(userInfoBean.getData().getNickname());
                Intent editIntent = new Intent(getActivity(), UsEditActivity.class);
                editIntent.putExtra("userData",mySerializable);
                startActivity(editIntent);
            }
        });
        bodyInfo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent editIntent = new Intent(getActivity(), BodyDataActivity.class);
                startActivity(editIntent);
            }
        });
        buttonExit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SharedPreferences.Editor editor = MyApplication.getContext().getSharedPreferences("data",MODE_PRIVATE).edit();
//                editor.putString("token",token);
                editor.remove("token");
                editor.apply();
                Intent exitIntent = new Intent(getActivity(), SplashActivity.class);
                startActivity(exitIntent);
                getActivity().finish();
            }
        });
        new UserGetInfo().get(new ApiListener() {
            @Override
            public void success(ApiUtil apiUtil) {
                UserGetInfo api = (UserGetInfo)apiUtil;
                String response = api.mResponse;
                L.d(response);
                 userInfoBean = new Gson().fromJson(response, UserInfoBean.class);
                user_name.setText(userInfoBean.getData().getNickname());
                Glide.with(getContext()).load(userInfoBean.getData().getHeader()).into(user_header);
            }

            @Override
            public void failure(ApiUtil apiUtil) {
            }
        });
    }
}
